package com.njbdqn.threetab.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.njbdqn.componse.page.PageInParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.njbdqn.threetab.domain.Userinfos;
import com.njbdqn.threetab.services.UserPowerService;

@Controller
public class InitController {
	@Autowired
	private UserPowerService upService;
	@InitBinder
	public void dateBind(ServletRequestDataBinder bind){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		df.setLenient(true);
		bind.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
	}
	
	@RequestMapping("/init.do")
	public String init(PageInParam pip,Map map){
		map.put("apow", upService.findAllPower());
		map.put("pb",upService.findBean(pip));
		return "index";
	}
	
	@RequestMapping("/updatePow.do")
	@ResponseBody
	public String upow(int [] pows){
//		upService.updateUserPower(user);
		System.out.println(pows.length);
		return "{success:'ok'}";
	}
	
	@RequestMapping("/single.do")
	@ResponseBody
	public Userinfos findUser(int userid){
		return upService.findUserById(userid);
	}
	
	/**
	 * 
	 * @param user
	 * @return {username:sss,password:xxx}
	 * 			null
	 */
	@RequestMapping("/login.do")
	@ResponseBody
	public Userinfos login(Userinfos user){
		System.out.println(user.getUsername()+user.getPassword());
		return upService.login(user);
	}
	
	
}
